<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getAdvancedDictionaryTypes">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionaryTypes" @row-dblclick="getClick" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="数据字典名" width="200" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="200" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total, sizes, prev, pager, next" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10, 15, 30, 50]" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="数据字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <el-drawer
        title="数据字典明细表"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="dictionaryDetails" highlight-current-row v-loading="drawerListLoading" @selection-change="drawerSelsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="200">
        </el-table-column>
        <el-table-column prop="name" label="明细名" width="200" sortable>
        </el-table-column>
        <el-table-column label="操作">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="drawerHandleAdd(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="drawerHandleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="drawerHandleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="drawerBatchRemove" :disabled="this.drawerSels.length===0">批量删除</el-button>
        <el-pagination layout="total, sizes, prev, pager, next" @size-change="drawerHandleSizeChange" @current-change="drawerHandleCurrentChange" :page-sizes="[5, 10, 15, 30, 50]" :current-page="drawerCurrentPage" :page-size="drawerPageSize" :total="drawerTotal" style="float:right;">
        </el-pagination>
      </el-col>
<!--字典明细表编辑界面-->
      <el-dialog :title="title" :visible.sync="drawerEditFormVisible" :close-on-click-modal="false" append-to-body>
        <el-form :model="drawerEditForm" label-width="80px" :rules="drawerEditFormRules" ref="drawerEditForm">
          <el-form-item label="明细名" prop="name">
            <el-input v-model="drawerEditForm.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="drawerEditFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="drawerEditSubmit" :loading="drawerEditLoading">提交</el-button>
        </div>
      </el-dialog>

    </el-drawer>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
        drawerEditFormVisible: false,
        typeId:'',
        drawerTotal:0,
        drawerCurrentPage: 1,

        drawerPageSize: 5,
        dictionaryDetails:[],
        drawerListLoading: false,
        table:false,
        dictionaryTypes:[],
				filters: {
					keyword: ''
				},
        title:"",
				total: 0,

				currentPage: 1,
        pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列
        drawerSels: [],
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
        drawerEditLoading: false,
				editFormRules: {
					username: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
          phone: [
            { required: true, message: '请输编号', trigger: 'blur' }
          ]
				},
        drawerEditFormRules:{
          name: [
            { required: true, message: '请输入明细名', trigger: 'blur' }
          ]
        },
				//编辑界面数据
				editForm: {
          id: null,
          name: '',
          sn: ''
				},
        drawerEditForm:{
          id: null,
          name:'',
          types_id:''
        }
			}
		},
		methods: {
      // 显示新增界面
      drawerHandleAdd(){
        this.drawerEditForm = {
          id: null,
          name:'',
          types_id:''
        }
        this.title = "新增";
        this.drawerEditFormVisible = true;
      },
      drawerEditSubmit(){
        this.$refs.drawerEditForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.drawerEditLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.drawerEditForm);
              para.types_id = this.typeId
              console.log(para)
              this.$http.put("/deta",para)
                  .then(result=>{
                    this.drawerEditFormVisible = false;
                    this.drawerEditLoading = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.drawerCurrentPage = 1;
                    this.getDictionaryDetail();
                  }).catch(result=>{
                this.drawerEditLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试",
                  type: 'error'
                });
              })
            });
          }
        });

      },
      // 显示编辑界面
      drawerHandleEdit(index, row){
        this.title = "编辑";
        this.drawerEditFormVisible = true;
        this.drawerEditForm = Object.assign({}, row);
      },
      drawerHandleDel(index, row){
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.drawerListLoading = true;
          //NProgress.start();
          this.$http.delete("/deta/"+row.id)
              .then(result=>{
                this.drawerListLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.drawerCurrentPage = 1;
                this.getDictionaryDetail();
              })
              .catch(result=>{
                this.drawerListLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试",
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
      drawerBatchRemove(){
        var ids = this.drawerSels.map(item => item.id)
        console.log(ids)
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.drawerListLoading = true;
          //NProgress.start();
          this.$http.patch("/deta", ids)
              .then(result=>{
                this.drawerListLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.drawerCurrentPage = 1;
                this.getDictionaryDetail();
              })
              .catch(result=>{
                this.drawerListLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试",
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      },
      drawerHandleSizeChange(val){
        this.drawerCurrentPage=1;
        this.drawerPageSize = val;
        this.getDictionaryDetail();
      },
      drawerHandleCurrentChange(val){
        this.drawerCurrentPage = val;
        this.getDictionaryDetail();
      },
      getClick(row){
        this.typeId = row.id;

        this.table = true;
        this.drawerListLoading = true;
        // 发送axios请求获取数据字典明细
        this.getDictionaryDetail();
      },
      getDictionaryDetail(){
        let para = {
          types_id: this.typeId,
          currentPage: this.drawerCurrentPage,
          pageSize: this.drawerPageSize,
          keyword: this.filters.keyword
        };
        this.$http.post("/deta", para)
            .then(result=>{
              console.log(para)
              this.drawerListLoading = false;

              this.dictionaryDetails = result.data.rows;
              this.drawerTotal = result.data.total;
            })
            .catch(request=>{
              this.$message({
                message: '系统繁忙，请稍后重试！',
                type: 'error'
              });
            })
      },
      handleSizeChange(val){
        // console.log(val)
        this.currentPage=1;
        this.pageSize = val;
        this.getDictionaryTypes();
      },
      // 高级查询数据字典类型
      getAdvancedDictionaryTypes(){
        this.currentPage = 1;
        this.getDictionaryTypes()
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryTypes();
			},
      getDictionaryTypes(){
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
        };
        this.listLoading = true;
        this.$http.post("/dict",para)
            .then(result=>{
              this.listLoading = false;
              this.dictionaryTypes = result.data.rows;
              this.total = result.data.total;
            })
            .catch(request=>{
              this.$message({
                message: '系统繁忙，请稍后重试！',
                type: 'error'
              });
            })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {

					this.listLoading = true;
					//NProgress.start();
					this.$http.delete("/dict/"+row.id)
              .then(result=>{
                this.listLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                this.getDictionaryTypes();
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试",
                  type: 'error'
                });
              })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = "编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.editFormVisible = true;
				this.editForm = {
          id: null,
					name: '',
          sn: '',
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$http.put("/dict",para)
                  .then(result=>{
                    this.editFormVisible = false;
                    this.editLoading = false;
                    if(result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.currentPage = 1;
                    this.getDictionaryTypes();
                  }).catch(result=>{
                      this.editLoading = false;
                      this.$message({
                        message: "系统繁忙，请稍后重试",
                        type: 'error'
                      });
                  })
							});
						}
        });
      },
			selsChange: function (sels) {
				this.sels = sels;
			},
      drawerSelsChange (sels) {
        this.drawerSels = sels;
      },
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
        console.log(ids)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          this.$http.patch("/dict", ids)
              .then(result=>{
                this.listLoading = false;
                if(result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                this.getDictionaryTypes();
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试",
                  type: 'error'
                });
              })
				}).catch(() => {

				});
			}
		},mounted() {
      this.getDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>